<?php 
class halaman_member extends CI_controller{

	function __construct(){
		parent::__construct();

		$this->load->helper('bara_helper');
		$this->load->library('bara_lib');

		$this->load->library('recaptcha');

		$this->load->model('modul/halaman_member_model');


		$this->load->model('modul/model_menu');

		$this->load->model('modul/about_model');
		$this->load->model('modul/article_model');
		$this->load->model('modul/sosmed_model');

	}

	function index($id=NULL){

		$data['text_home'] = "Home";
		$data['text_title'] = "Member Zone";

		$logged_in = $this->session->userdata('logged_in');
        $id_member = $logged_in['id_member'];


		$data['base_url'] = base_url().'modul/wisata/index';
		$data['total_rows'] = $this->halaman_member_model->get_wisata_rows($this->bara_lib->language());
		$data['per_page'] = 16;

		$data['total_page'] = ceil($data['total_rows']/$data['per_page']);

		if($id==null){
			$id=1;
			$data['offset'] = 0; 
		} else {
			$id=$id;
			$data['offset'] = ($id-1) * $data['per_page'];	
		}  

		$data['posisi'] = $id;


		$data['gets_wisata'] = $this->halaman_member_model->get_wisata($this->bara_lib->language(),$data['per_page'],$data['offset']);

		$data['menu_member'] = 'frontend/'. $this->bara_lib->theme() .'/modul/halaman_member/menu_member';

		$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/halaman_member/index';

		//menu
		$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());

		// sosmed
		$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
		$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
		$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();

		//footer
		$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
		$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
		//end footer

		$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
		$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
		$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
		$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);

	}

	function view($id){

		$data['text_home'] = "Home";
		$data['text_title'] = "wisata";

		//coment
		$this->recaptcha->recaptcha_check_answer();

		if ($this->input->post() && $this->recaptcha->getIsValid()) {
			
			$date = get_date_time();

			$data_comment = array(
				'id_wisata' => $id,
				'date_update' => $date,
				'name' => $this->input->post('name'),
				'email' => $this->input->post('email'),
				'url' =>$this->input->post('url'),
				'content' =>$this->input->post('comment'),
				'status' => 0
				);

			$id_menu = $this->db->insert('comment', $data_comment);
			
			$data['benar'] = "<div class='alert success'><i class='icon-success'></i> This is a Success Comment! <span class='alert-close fa-times'></span></div>";
        } else if ($this->input->post() && !$this->recaptcha->getIsValid()) {
        	$data['benar'] = "<div class='alert error'><i class='icon-error'></i> An Error Comment! <span class='alert-close fa-times'></span></div>";
        } else {
        	$data['benar'] = "";
        }

        $data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();

        $data['get_comments'] = $this->halaman_member_model->get_comment($id);
        $data['get_row_comment'] = $this->halaman_member_model->get_comment_row($id); 

        $data['get_foto_slide'] = $this->halaman_member_model->get_foto_slide($id);

		$data['get_wisata_views'] = $this->halaman_member_model->get_wisata_view($this->bara_lib->language(),$id);

		$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/wisata/view';
		

		//menu
		$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());
		
		// sosmed
		$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
		$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
		$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();
		
		//footer
		$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
		$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
		//end footer
		
		$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
		$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
		$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
		$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);	
	}

	function rate()
	{

		$this->load->view('frontend/'. $this->bara_lib->theme() .'/modul/wisata/rate');
	}

	function edit(){
		$data['text_home'] = "Home";
		$data['text_title'] = "Edit Profile";


		$logged_in = $this->session->userdata('logged_in');
        $username_member = $logged_in['username_member'];

		$where = array('username_member' => $username_member);
		$data['member'] = $this->user->edit('member', $where);
		
		$data['menu_member'] = 'frontend/'. $this->bara_lib->theme() .'/modul/halaman_member/menu_member';
		
		$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/halaman_member/edit_profile';
		//menu
		$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());

		// sosmed
		$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
		$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
		$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();

		//footer
		$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
		$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
		//end footer

		$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
		$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
		$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
		$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);   
	}

	function update(){
		
		$data['text_home'] = "Home";
		$data['text_title'] = "Edit Profile";

		$logged_in = $this->session->userdata('logged_in');
        $username_member = $logged_in['username_member'];

		//validation
		
		$this->form_validation->set_rules('nama_member', 'nama member', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('alamat_member', 'Alamat Member', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('kontak_member', 'Kontak Member', 'trim|required|min_length[3]|xss_clean');
		
		  

		if ($this->form_validation->run() == FALSE)
		{
			$id_member = $this->input->post('id_member');

			$where = array('username_member' => $username_member);
			$data['member'] = $this->user->edit('member', $where);

			$data['menu_member'] = 'frontend/'. $this->bara_lib->theme() .'/modul/halaman_member/menu_member';	
			$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/halaman_member/edit_profile';

			//menu
			$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());

			// sosmed
			$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
			$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
			$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();

			//footer
			$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
			$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
			//end footer

			$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
			$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
			$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
			$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data); 

			$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data); 
		}else{

			$date = get_date_time();
			$config['remove_spaces']  = TRUE;
			$config['upload_path'] = './uploads/member/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';

			$this->load->library('upload', $config);

			if ($this->upload->do_upload()) {
				$data = array('upload_data' => $this->upload->data());
				$file = $_FILES["userfile"]["name"];
				$name = $this->input->post('name');


				$base = base_url();

				$path_to_file = $this->bara_lib->url_dir().$this->input->post('gambar');
				

				unlink($path_to_file);
				$file = str_replace(' ', '_', $file);

				$datetime = strtotime(get_date_time());
				$data_member = array(
					'foto_member' => "uploads/member/".$file,
					'nama_member' => $this->input->post('nama_member'),
					'alamat_member' => $this->input->post('alamat_member'),
					'kontak_member' => $this->input->post('kontak_member'),
					);

				$url = $this->bara_lib->url_dir();

				rename($url.'uploads/member/'.$file, $url.'uploads/member/'.$file);
			}
			else{
				$data_member = array(
					'nama_member' => $this->input->post('nama_member'),
					'alamat_member' => $this->input->post('alamat_member'),
					'kontak_member' => $this->input->post('kontak_member'),
					);
			}

			$where_member = array('username_member' => $username_member);
			$this->user->update('member',$where_member, $data_member);

			


			redirect('modul/halaman_member/edit');
		}

	}
}
?>